package connector

import (
	"connector/game"
	"connector/net"
	"fmt"
)

type Connector struct {
	isRunning bool
	wsManager *net.WsManager
}

func Default() *Connector {
	return &Connector{
		wsManager: net.NewWsManager(),
	}
}

func (c *Connector) Run(serverId string) {
	if !c.isRunning {
		// 1. 启动 WebSocket 服务
		connectorConfig := game.Conf.GetConnector(serverId)
		addr := fmt.Sprintf("%s:%d", connectorConfig.Host, connectorConfig.ClientPort)
		c.wsManager.ServerId = serverId
		c.isRunning = true
		c.wsManager.Run(addr)
	}
}

// Close 关闭服务
func (c *Connector) Close() {
	if c.isRunning {
		c.wsManager.Close()
		c.isRunning = false
	}
}
